package persistance.database.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import persistance.hibernate.InitSessionFactory;
import domain.valueObejcts.Floor;
import domain.valueObejcts.Location;

public class LocationDAO {

	private static Session session;
	
	@SuppressWarnings("unchecked")
	public static List<Location> read(HashMap<String, String> filter) {
		List<Location> result = new ArrayList<Location>();
		List<String> list = new ArrayList<String>();
		
		for (Map.Entry<String, String> entry : filter.entrySet()){
			String column = entry.getKey();
			list.add(column + " = :" + column);
		}
		
		StringBuilder sb = new StringBuilder();
		if(filter.size() > 0){
			sb.append("WHERE ");
			sb.append(list.remove(0));
			for (String s : list) {
				sb.append(" AND ");
				sb.append(s);
			}
		}
		
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		Query query = session.createQuery("FROM Location " + sb.toString());
		
		for (Map.Entry<String, String> entry : filter.entrySet()){
			String column = entry.getKey();
			String value = entry.getValue();
			query.setParameter(column, value);
		}
		
		result = query.list();
		tx.commit();
		filter.clear();
		
		return result;
	}

	public static void create() {
		Location loc = null;
		Floor floor = null;
		List<Location> locs = new ArrayList<Location>();
		for (int i = 1; i <= 5; i++) {
			floor = FloorDAO.read(i).get(0);
			loc = new Location();
			loc.setCountPersons(2);
			loc.setFloor(floor);
			loc.setNumber(Location.FIRST);
			loc.setType(Location.BAR);
			loc.setxPos(100);
			loc.setyPos(100);
			locs.add(loc);
			
			loc = new Location();
			loc.setCountPersons(1);
			loc.setFloor(floor);
			loc.setNumber(Location.SECOND);
			loc.setType(Location.BAR);
			loc.setxPos(500);
			loc.setyPos(100);
			locs.add(loc);
			
			loc = new Location();
			loc.setCountPersons(1);
			loc.setFloor(floor);
			loc.setNumber(Location.THIRD);
			loc.setType(Location.BAR);
			loc.setxPos(300);
			loc.setyPos(300);
			locs.add(loc);
			
			loc = new Location();
			loc.setCountPersons(1);
			loc.setFloor(floor);
			loc.setNumber(Location.FIRST);
			loc.setType(Location.JUMPER);
			loc.setxPos(0);
			loc.setyPos(0);
			locs.add(loc);
			
			loc = new Location();
			loc.setCountPersons(1);
			loc.setFloor(floor);
			loc.setNumber(Location.SECOND);
			loc.setType(Location.JUMPER);
			loc.setxPos(0);
			loc.setyPos(200);
			locs.add(loc);
		}
		
		loc = new Location();
		loc.setCountPersons(1);
		loc.setFloor(null);
		loc.setNumber(Location.FIRST);
		loc.setType(Location.SUPER_VISIOR);
		loc.setxPos(0);
		loc.setyPos(0);
		locs.add(loc);
		
		session = InitSessionFactory.getInstance().getCurrentSession();
		Transaction tx = session.beginTransaction();
		for (Location location : locs) {
			session.save(location);
		}
		
		tx.commit();
	}

}
